//
//  CMRequestConstructor.m
//  CocoaMesh
//
//  Created by George  Armah on 4/29/09.
//  Copyright 2009 Lafayette College. All rights reserved.
//

#import "CMRequestConstructor.h"


@implementation CMRequestConstructor



+(NSMutableURLRequest*)windowsLiveIDRequest:(NSString*)userName 
								   password:(NSString*)pwd
							  applicationID:(NSString*)appID
{
	//Create SOAP Message
	NSNumber * rand = [NSNumber numberWithInt:( 10000 + (random()% (99999 - 10000)))];
	NSDate * current = [NSDate date];
	NSDate * currentPlusHour = [current addTimeInterval:3600];
	NSString * soapMessage = [NSString stringWithFormat:LiveFrameworkSOAPTemplate,
							  appID,
							  LiveIDSOAPURLEndPoint,
							  [rand stringValue],
							  userName,
							  pwd,
							  [current description],
							  [currentPlusHour description],
							  LiveFXDomain];
	
	
	NSDictionary * hFields = [NSDictionary dictionaryWithObjectsAndKeys:
	HTTP_APPSOAPXML_CHARSETUTF8, HTTP_CONTENT_TYPE, 
	[NSString stringWithFormat:@"%d", [soapMessage length]], HTTP_CONTENT_LENGTH, nil];
	
	
	//NSLog(soapMessage);
	return [self requestWithURL:[NSURL URLWithString:LiveIDSOAPURLEndPoint] 
				   headerFields:hFields HTTPVerb:HTTP_POST 
					   HTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
}


+(NSMutableURLRequest *)CLOETopLevelRequest:(NSString*)authT 
{
	NSDictionary * hFields = [NSDictionary dictionaryWithObjectsAndKeys:
							  HTTP_APPJSON, HTTP_CONTENT_TYPE, 
							  HTTP_APPJSON, HTTP_ACCEPT, 
							  LiveFXDomain, HTTP_HOST,
							  authT, HTTP_AUTHORIZATION, nil];
	
	
	return [self requestWithURL:[NSURL URLWithString:LiveFXBaseURI] 
				   headerFields:hFields 
					   HTTPVerb:HTTP_GET 
					   HTTPBody:nil];
}


+(NSMutableURLRequest *)meshResourceGETRequest:(NSString*)authT withURL:(NSURL*)url
{
	NSDictionary * hFields = [NSDictionary dictionaryWithObjectsAndKeys:
								HTTP_APPJSON, HTTP_CONTENT_TYPE,
								HTTP_APPJSON, HTTP_ACCEPT,
								LiveFXDomain, HTTP_HOST,
							  authT, HTTP_AUTHORIZATION,
							  nil];
	
	
	return [self requestWithURL:url 
					headerFields:hFields 
					   HTTPVerb:HTTP_GET 
					   HTTPBody:nil];
}


+(NSMutableURLRequest *)meshResourcePUTRequest:(NSString*)authT withURL:(NSURL*)url HTTPBody:(NSData*)body
{
	NSDictionary * hFields = [NSDictionary dictionaryWithObjectsAndKeys:
							  HTTP_APPJSON, HTTP_CONTENT_TYPE,
							  HTTP_APPJSON, HTTP_ACCEPT,
							  LiveFXDomain, HTTP_HOST,
							  authT, HTTP_AUTHORIZATION,
							  nil];
	
	
	return [self requestWithURL:url 
				   headerFields:hFields 
					   HTTPVerb:HTTP_PUT 
					   HTTPBody:body];
}


+(NSMutableURLRequest *)meshResourceDELETERequest:(NSString*)authT withURL:(NSURL*)url
{
	NSDictionary * hFields = [NSDictionary dictionaryWithObjectsAndKeys:
							  HTTP_APPJSON, HTTP_CONTENT_TYPE,
							  HTTP_APPJSON, HTTP_ACCEPT,
							  LiveFXDomain, HTTP_HOST,
							  authT, HTTP_AUTHORIZATION,
							  nil];
	
	return [self requestWithURL:url 
				   headerFields:hFields 
					   HTTPVerb:HTTP_DELETE 
					   HTTPBody:nil];
}


+(NSMutableURLRequest*)requestWithURL:(NSURL*)url 
						 headerFields:(NSDictionary*)hFields 
							 HTTPVerb:(NSString*)verb 
							 HTTPBody:(NSData*)body
{
	NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
	[request setHTTPMethod:verb];
	[request setHTTPBody:body];
	
	for (NSString * headerField in hFields)
	{
		[request setValue:[hFields valueForKey:headerField] forHTTPHeaderField:headerField];
	}
	
	return request;
}


+(NSMutableURLRequest *)meshResourcePOSTRequest:(NSString*)authT withURL:(NSURL*)url HTTPBody:(NSData*)body
{
	NSDictionary * hFields = [NSDictionary dictionaryWithObjectsAndKeys:
							  HTTP_APPJSON, HTTP_CONTENT_TYPE,
							  HTTP_APPJSON, HTTP_ACCEPT,
							  LiveFXDomain, HTTP_HOST,
							  authT, HTTP_AUTHORIZATION,
							  nil];
	
	return [self requestWithURL:url 
				   headerFields:hFields 
					   HTTPVerb:HTTP_POST 
					   HTTPBody:body];
}

@end
